package com.sugree.util.parser;

import java.io.InputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.text.ParseException;

import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
/*
import org.kxml2.io.KXmlParser;
*/

import com.sugree.util.entity.Message;

import android.util.Log;

public class RssPullParser extends BaseFeedParser {
	public final static String TAG = "RssPull";

	private List<Message> mMessages;
	private XmlPullParser mParser;
	private Exception mException;
	private String mEncoding;

	public RssPullParser(InputStream is) {
		super(is);
	}

	public RssPullParser(InputStream is, String encoding) {
		super(is);
		mEncoding = encoding;
	}

	@Override
	public List<Message> parse() {
		mMessages = new ArrayList<Message>();
		try {
			mException = null;
			XmlPullParser parser = createParser();
			parser.setInput(mInputStream, mEncoding);
			parse(parser);
		} catch (IOException e) {
			Log.e(TAG, "IOException", e);
			mException = e;
		} catch (XmlPullParserException e) {
			Log.e(TAG, "XmlPullParserException", e);
			mException = e;
		} catch (RuntimeException e) {
			Log.e(TAG, "RuntimeException", e);
			mException = e;
		}
		return mMessages;
	}

	protected void parse(XmlPullParser parser) throws IOException, XmlPullParserException {
		Message message = null;
		String name = "";

		int eventType = parser.getEventType();
		while (eventType != XmlPullParser.END_DOCUMENT) {
			switch(eventType) {
			case XmlPullParser.START_TAG:
				name = parser.getName();
				//Log.d(TAG, "start "+name);
				if (name.equals(ITEM)) {
					message = new Message();
				}
				if (message != null) {
					if (name.equals(TITLE)) {
						message.setTitle(parser.nextText());
					} else if (name.equals(DESCRIPTION)) {
						message.setDescription(parser.nextText());
					} else if (name.equals(LINK)) {
						message.setLink(parser.nextText());
					} else if (name.equals(PUB_DATE)) {
						message.setPubDate(parser.nextText());
					} else if (name.equals(CREATOR)) {
						message.setAuthor(parser.nextText());
					} else if (name.equals(ENCLOSURE)) {
						message.addEnclosure(parser.getAttributeValue(null, "url"));
					}
				}
				break;

			case XmlPullParser.END_TAG:
				name = parser.getName();
				//Log.d(TAG, "end "+name);
				if (name.equals(ITEM)) {
					mMessages.add(message);
					message = null;
				}
				break;
			}
			eventType = parser.next();
		}
	}

	protected XmlPullParser createParser() throws XmlPullParserException {
		/*
		XmlPullParser parser = new KXmlParser();
		parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
		parser.setFeature("http://xmlpull.org/v1/doc/features.html#relaxed", true);
		*/
		XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
		factory.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
		factory.setFeature("http://xmlpull.org/v1/doc/features.html#relaxed", true);
		factory.setValidating(false);
		XmlPullParser parser = factory.newPullParser();
		return parser;
	}

	public Exception getException() {
		return mException;
	}
}
